//
// Created by wangxiaodong on 2017/11/25.
//

#ifndef SELECTIONSORT_SORTTESTHELPER_H
#define SELECTIONSORT_SORTTESTHELPER_H

#include <ostream>
#include <ctime>
#include <cassert>
#include <string>

using namespace std;

namespace SortTestHelper {

    /**
     * 生成随机数组 [lRange,rRange]
     * @param n
     * @param lRange
     * @param rRange
     * @return
     */
    int *generateRandomArray(int n, int lRange, int rRange) {

        assert(lRange <= rRange);

        int *arr = new int[n];
        // 随机种子设置
        srand(time(NULL));
        for (int i = 0; i < n; ++i) arr[i] = rand() % (rRange - lRange + 1) + lRange;

        return arr;
    }

    /**
     * 生成n个只有stamp个无序的数组
     * @param n
     * @param stamp
     * @return
     */
    int *generateAlmostOrderArray(int n,int stamp){
        int *arr = new int[n];
        for (int i = 0; i < n; ++i) {
            arr[i] = i;
        }

        srand(time(NULL));
        for (int j = 0; j < stamp; ++j) {
            int index1 = rand() % n;
            int index2 = rand() % n;
            swap(arr[index1],arr[index2]);
        }
        return arr;
    }


    template<typename T>
    void printArray(T arr[], int len) {
        for (int i = 0; i < len; ++i) cout << arr[i] << " ";
        cout << endl;
    }

    template<typename T>
    bool isSorted(T arr[], int len) {
        for (int i = 0; i < len - 1; ++i)
            if (arr[i] > arr[i + 1]) return false;


        return true;
    }

    template<typename T>
    void testSort(string sortName, void(*sort)(T[], int), T arr[], int len) {

        clock_t start = clock();
        sort(arr, len);
        assert(isSorted(arr,len));

        cout << sortName << "\t\t\tconsuming\t\t\t" << double(clock() - start) / CLOCKS_PER_SEC << " s" << endl;
        return;
    }

    /**
     * 拷贝数组
     * @param arr
     * @param len
     * @return
     */
    int* copyArry(int arr[],int len){

        int* a = new int[len];
        copy(arr,arr + len,a);
        return a;
    }

}

#endif //SELECTIONSORT_SORTTESTHELPER_H
